× Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson 6 Lesson 7 Lesson 8 Lesson 9 Lesson 10 Lesson 11 Lesson 12 Lesson 13 Lesson 14 Lesson 15 Lesson 16

All lessons

Lesson 13 - Dictionaries

Dictionaries are a way of storing data in Python, like lists which we have gone over in a previous lesson.

Dictionaries function the same that a real dictionary would work, even though no one has used one in the past 5 years, where each term has a definition. In the case of python, the term is referred to as the key, and the definition is referred to as a value.

The syntax for a dictionary is as the following: dict = { 'a_number': 1, 'another_number': 2, }

We can use for loops to print out a dictionary, with the following syntax: for x in dict: print(x + ': ' + str(dict[x]))

This would output a dictionary key and its value seperated by a colon every line. You can also print out a value using the following syntax: print(dict['a_number']), which would print out 1

You can add on keys and values to your dictionary after it is initialized, created, using the following syntax: dict['new_key'] = 'new_value'

You can also get rid of a key or value, using the following syntax: del('dict'['new_key'])

To test your understanding of dictionaries, perform the following: Create a new dictionary with corresponding keys and values for your first and last name, your birthday, and what state you live in. Then, using the %s, print out a sentence introducing yourself. For example: Hi, my name is John Smith! My birthday is on 12/12/99 and I am from California, nice to meet you!

Once you have printed out something similiar, you can move on to the next lesson